package com.zbdx.medicalzb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbdx.medicalzb.entity.CompanyPolicyEntity;
import com.zbdx.medicalzb.mapper.CompanyPolicyMapper;
import com.zbdx.medicalzb.model.CompanyPolicyModel;
import com.zbdx.medicalzb.param.CompanyPolicyParam;
import com.zbdx.medicalzb.util.Msg;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CompanyPolicyService {
    @Autowired
    private CompanyPolicyMapper companyPolicyMapper;
    public Msg getAllPolicyWithPage(CompanyPolicyParam param) {
        if (param.getSize() == 0) {
            param.setSize(1);
        }
        PageHelper.startPage(param.getPn(), param.getSize());
        List<CompanyPolicyModel> list = companyPolicyMapper.getAllPolicy(param);
        PageInfo<CompanyPolicyModel> info = new PageInfo<>(list,5);
        return Msg.success().data("policyInfo",info);
    }

    public Msg updatePolicy(Long id, CompanyPolicyParam param) {
        CompanyPolicyEntity entity = new CompanyPolicyEntity();
        BeanUtils.copyProperties(param,entity);
        entity.setUpdateTime(new DateTime().toDate());
        entity.setId(id);
        int i = companyPolicyMapper.updatePolicy(entity);
        if (i > 0) {
            return Msg.success().mess("修改成功").data("updateData",entity);
        }
        return Msg.fail().mess("修改失败");
    }

    public Msg savePolicy(CompanyPolicyParam param) {
        CompanyPolicyEntity entity = new CompanyPolicyEntity();
        BeanUtils.copyProperties(param,entity);
        entity.setCreateTime(new DateTime().toDate());
        entity.setUpdateTime(new DateTime().toDate());
        int i = companyPolicyMapper.savePolicy(entity);
        if (i > 0) {
            return Msg.success().mess("新增成功").data("saveData",entity);
        }
        return Msg.fail().mess("新增失败");
    }

    public Msg deletePolicy(Long id) {
        int i = companyPolicyMapper.deletePolicy(id);
        if (i > 0) {
            return Msg.success().mess("删除成功").data("numberOfDelete",i);
        }
        return Msg.fail().mess("删除失败");

    }
}
